package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

import model.NouveauCompteModel;
import controller.ClientControler;

public class ModifierProfilView {

	private ClientControler controlleur;
	private NouveauCompteModel modele;

	private static javax.swing.JFrame frame;
	private javax.swing.JButton jButtonAnnuler;
	private javax.swing.JButton jButtonLoadImage;
	private javax.swing.JButton jButtonOK;
	private javax.swing.JLabel jLabelAvatarImage;
	private javax.swing.JLabel jLabelMotDePasse;
	private javax.swing.JLabel jLabelMotDePasseConf;
	private javax.swing.JLabel jLabelNewAccount;
	private javax.swing.JLabel jLabelNom;
	private javax.swing.JLabel jLabelPrenom;
	private javax.swing.JLabel jLabelPseudo;
	private javax.swing.JLabel jLabelVotreAvatar;
	private javax.swing.JPanel jPanelAvatar;
	private javax.swing.JPanel jPanelDonnees;
	private javax.swing.JPanel jPanelNewAccount;
	private javax.swing.JPanel jPanelOkAnnuler;
	private javax.swing.JPasswordField jPasswordFieldMotDePasse;
	private javax.swing.JPasswordField jPasswordFieldMotDePasseConf;
	private javax.swing.JTextField jTextFieldNom;
	private javax.swing.JTextField jTextFieldPrenom;
	private javax.swing.JTextField jTextFieldPseudo;
	private javax.swing.JFileChooser jFileChooser;

	public void showView() {
		this.frame.setVisible(true);
	}
	
	public void closeView() {
		this.frame.setVisible(false);
	}

	public ModifierProfilView(ClientControler controlleur, NouveauCompteModel nouveauCompteModel) {
		this.controlleur = controlleur;
		this.modele = nouveauCompteModel;
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		jPanelNewAccount = new javax.swing.JPanel();
		jLabelNewAccount = new javax.swing.JLabel();
		jPanelDonnees = new javax.swing.JPanel();
		jLabelPrenom = new javax.swing.JLabel();
		jTextFieldPrenom = new javax.swing.JTextField();
		jLabelNom = new javax.swing.JLabel();
		jTextFieldNom = new javax.swing.JTextField();
		jLabelPseudo = new javax.swing.JLabel();
		jTextFieldPseudo = new javax.swing.JTextField();
		jLabelMotDePasse = new javax.swing.JLabel();
		jPasswordFieldMotDePasse = new javax.swing.JPasswordField();
		jLabelMotDePasseConf = new javax.swing.JLabel();
		jPasswordFieldMotDePasseConf = new javax.swing.JPasswordField();
		jPanelAvatar = new javax.swing.JPanel();
		jButtonLoadImage = new javax.swing.JButton();
		jLabelVotreAvatar = new javax.swing.JLabel();
		jLabelAvatarImage = new javax.swing.JLabel();
		jPanelOkAnnuler = new javax.swing.JPanel();
		jButtonOK = new javax.swing.JButton();
		jButtonAnnuler = new javax.swing.JButton();
		jFileChooser = new javax.swing.JFileChooser();

		frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabelNewAccount.setFont(new java.awt.Font("Dialog", 0, 48)); // NOI18N
		jLabelNewAccount
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabelNewAccount.setText("Modifier mon profil");

		javax.swing.GroupLayout jPanelNewAccountLayout = new javax.swing.GroupLayout(
				jPanelNewAccount);
		jPanelNewAccount.setLayout(jPanelNewAccountLayout);
		jPanelNewAccountLayout.setHorizontalGroup(jPanelNewAccountLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						jPanelNewAccountLayout
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabelNewAccount,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										604,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		jPanelNewAccountLayout.setVerticalGroup(jPanelNewAccountLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						jPanelNewAccountLayout
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabelNewAccount)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		jLabelPrenom.setText("Prénom ");
		jTextFieldPrenom.setText(modele.getPrenom());

		// jTextFieldPrenom.addActionListener(new
		// java.awt.event.ActionListener() {
		// public void actionPerformed(java.awt.event.ActionEvent evt) {
		// jTextFieldPrenomActionPerformed(evt);
		// }
		// });

		jLabelNom.setText("Nom ");
		jTextFieldNom.setText(modele.getNom());

		jLabelPseudo.setText("Pseudo");
		jTextFieldPseudo.setText(modele.getPseudo());
		

		jLabelMotDePasse.setText("Mot de passe");
		jPasswordFieldMotDePasse.setText(modele.getPassword());

		jLabelMotDePasseConf.setText("Confirmer mot de passe");
		jPasswordFieldMotDePasseConf.setText(modele.getPassword());

		javax.swing.GroupLayout jPanelDonneesLayout = new javax.swing.GroupLayout(
				jPanelDonnees);
		jPanelDonnees.setLayout(jPanelDonneesLayout);
		jPanelDonneesLayout
				.setHorizontalGroup(jPanelDonneesLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelDonneesLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanelDonneesLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jLabelMotDePasseConf)
														.addComponent(
																jLabelMotDePasse)
														.addComponent(
																jLabelPrenom)
														.addComponent(jLabelNom)
														.addComponent(
																jLabelPseudo))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanelDonneesLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanelDonneesLayout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																		.addComponent(
																				jPasswordFieldMotDePasse,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				140,
																				Short.MAX_VALUE)
																		.addComponent(
																				jPasswordFieldMotDePasseConf,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				140,
																				Short.MAX_VALUE)
																		.addComponent(
																				jTextFieldPseudo))
														.addGroup(
																jPanelDonneesLayout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING,
																				false)
																		.addComponent(
																				jTextFieldNom,
																				javax.swing.GroupLayout.Alignment.LEADING)
																		.addComponent(
																				jTextFieldPrenom,
																				javax.swing.GroupLayout.Alignment.LEADING,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				140,
																				Short.MAX_VALUE)))
										.addContainerGap(27, Short.MAX_VALUE)));
		jPanelDonneesLayout
				.setVerticalGroup(jPanelDonneesLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelDonneesLayout
										.createSequentialGroup()
										.addGroup(
												jPanelDonneesLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabelPrenom)
														.addComponent(
																jTextFieldPrenom,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanelDonneesLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabelNom)
														.addComponent(
																jTextFieldNom,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanelDonneesLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabelPseudo)
														.addComponent(
																jTextFieldPseudo,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanelDonneesLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabelMotDePasse)
														.addComponent(
																jPasswordFieldMotDePasse,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanelDonneesLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabelMotDePasseConf)
														.addComponent(
																jPasswordFieldMotDePasseConf,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		jButtonLoadImage.setText("Sélectionner une image");
		jButtonLoadImage.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int returnVal = jFileChooser.showOpenDialog(ModifierProfilView
						.getFrame());
				if (returnVal == JFileChooser.APPROVE_OPTION) {
	                File file = jFileChooser.getSelectedFile();
	                controlleur.saveAvatar(file);
	                controlleur.updateAvatar();
	            } else {
	                
	            }
			}
		});

		jLabelVotreAvatar.setFont(new java.awt.Font("Dialog", 0, 24));
		jLabelVotreAvatar.setText("Votre avatar");

//		jLabelAvatarImage.setIcon(new javax.swing.ImageIcon(getClass().getResource(modele.getPathToAvatar())));

		javax.swing.GroupLayout jPanelAvatarLayout = new javax.swing.GroupLayout(
				jPanelAvatar);
		jPanelAvatar.setLayout(jPanelAvatarLayout);
		jPanelAvatarLayout
				.setHorizontalGroup(jPanelAvatarLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelAvatarLayout
										.createSequentialGroup()
										.addGroup(
												jPanelAvatarLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanelAvatarLayout
																		.createSequentialGroup()
																		.addGap(80,
																				80,
																				80)
																		.addComponent(
																				jLabelAvatarImage))
														.addGroup(
																jPanelAvatarLayout
																		.createSequentialGroup()
																		.addGap(50,
																				50,
																				50)
																		.addComponent(
																				jButtonLoadImage))
														.addGroup(
																jPanelAvatarLayout
																		.createSequentialGroup()
																		.addGap(65,
																				65,
																				65)
																		.addComponent(
																				jLabelVotreAvatar)))
										.addContainerGap(39, Short.MAX_VALUE)));
		jPanelAvatarLayout.setVerticalGroup(jPanelAvatarLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						jPanelAvatarLayout
								.createSequentialGroup()
								.addComponent(jLabelVotreAvatar)
								.addGap(18, 18, 18)
								.addComponent(jLabelAvatarImage)
								.addGap(18, 18, 18)
								.addComponent(jButtonLoadImage)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		jButtonOK.setText("OK");
		jButtonOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controlleur.retrieveModifiedUserInfo(jTextFieldPrenom.getText(),
						jTextFieldNom.getText(), jTextFieldPseudo.getText(),
						new String(jPasswordFieldMotDePasse.getPassword()),
						new String(jPasswordFieldMotDePasseConf.getPassword()));
			}
		});

		jButtonAnnuler.setText("Annuler");
		jButtonAnnuler.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controlleur.showMainView();
			}
		});

		javax.swing.GroupLayout jPanelOkAnnulerLayout = new javax.swing.GroupLayout(
				jPanelOkAnnuler);
		jPanelOkAnnuler.setLayout(jPanelOkAnnulerLayout);
		jPanelOkAnnulerLayout
				.setHorizontalGroup(jPanelOkAnnulerLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelOkAnnulerLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jButtonOK,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												90,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jButtonAnnuler,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												90,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		jPanelOkAnnulerLayout
				.setVerticalGroup(jPanelOkAnnulerLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelOkAnnulerLayout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												jPanelOkAnnulerLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jButtonOK)
														.addComponent(
																jButtonAnnuler))));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				frame.getContentPane());
		frame.getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(42, 42,
																		42)
																.addComponent(
																		jPanelDonnees,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		jPanelAvatar,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addComponent(
														jPanelNewAccount,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														593,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(237, Short.MAX_VALUE)
								.addComponent(jPanelOkAnnuler,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(235, 235, 235)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jPanelNewAccount,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(27, 27, 27)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addComponent(
																		jPanelDonnees,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(46, 46,
																		46))
												.addComponent(
														jPanelAvatar,
														javax.swing.GroupLayout.Alignment.TRAILING,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jPanelOkAnnuler,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		frame.pack();
	}

	public static javax.swing.JFrame getFrame() {
		return frame;
	}

	public void setFrame(javax.swing.JFrame frame) {
		this.frame = frame;
	}

	public ClientControler getControlleur() {
		return controlleur;
	}

	public void setControlleur(ClientControler controlleur) {
		this.controlleur = controlleur;
	}

	public javax.swing.JPasswordField getjPasswordFieldMotDePasse() {
		return jPasswordFieldMotDePasse;
	}

	public void setjPasswordFieldMotDePasse(
			javax.swing.JPasswordField jPasswordFieldMotDePasse) {
		this.jPasswordFieldMotDePasse = jPasswordFieldMotDePasse;
	}

	public javax.swing.JPasswordField getjPasswordFieldMotDePasseConf() {
		return jPasswordFieldMotDePasseConf;
	}

	public void setjPasswordFieldMotDePasseConf(
			javax.swing.JPasswordField jPasswordFieldMotDePasseConf) {
		this.jPasswordFieldMotDePasseConf = jPasswordFieldMotDePasseConf;
	}

	public javax.swing.JTextField getjTextFieldNom() {
		return jTextFieldNom;
	}

	public void setjTextFieldNom(javax.swing.JTextField jTextFieldNom) {
		this.jTextFieldNom = jTextFieldNom;
	}

	public javax.swing.JTextField getjTextFieldPrenom() {
		return jTextFieldPrenom;
	}

	public void setjTextFieldPrenom(javax.swing.JTextField jTextFieldPrenom) {
		this.jTextFieldPrenom = jTextFieldPrenom;
	}

	public javax.swing.JTextField getjTextFieldPseudo() {
		return jTextFieldPseudo;
	}

	public void setjTextFieldPseudo(javax.swing.JTextField jTextFieldPseudo) {
		this.jTextFieldPseudo = jTextFieldPseudo;
	}

	public javax.swing.JPanel getjPanelAvatar() {
		return jPanelAvatar;
	}

	public void setjPanelAvatar(javax.swing.JPanel jPanelAvatar) {
		this.jPanelAvatar = jPanelAvatar;
	}

	public javax.swing.JLabel getjLabelAvatarImage() {
		return jLabelAvatarImage;
	}

	public void setjLabelAvatarImage(javax.swing.JLabel jLabelAvatarImage) {
		this.jLabelAvatarImage = jLabelAvatarImage;
	}
	
}
